﻿using HuaCheng.CommonClass;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    /// <summary>
    /// 批量生成电池条码电池条码
    /// </summary>
    public partial class BatchGenerBarcode : Form
    {
        #region 页面初始化
        public static int startNum;//批量生成起始序号
        public static int endNum;//批量生成结束序号
        public static string startBar;//起始条码
        public static List<Channel> chlList;//通道数据类集合
        public delegate void WeekBarcodeEdit();//定义委托 委托主界面执行编辑表
        public event WeekBarcodeEdit UpdateTreeList;//定义事件 
        /// <summary>
        /// 批量生成条码页面初始化
        /// </summary>
        /// <param name="s">起始序号</param>
        /// <param name="chlist">通道数据类</param>
        public BatchGenerBarcode(string s,List<Channel> chlist)
        {
            try
            {
                InitializeComponent();//初始化元件
                this.txbStartNum.Text = s;//起始序号为treeview中选中行号
                this.txbEndNum.Text = s;//默认终止序号等于起始序号
                startNum = Convert.ToInt32(s);//初始化起始序号
                endNum = Convert.ToInt32(s);//初始化终止序号
                chlList = chlist;//初始化通道数据类
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);

            }
        }
        #endregion
        #region 确定与取消按钮点击事件
        /// <summary>
        /// 点击确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            string end = this.txbEndNum.Text.Trim();
            startBar = this.txbStartBar.Text.Trim();
            if (CheckEndNum(end)&&CheckBarcode(startBar))//如果终止序号正确且条码输入正确
            {
                if (CheckBarcordExisted(startBar))//如果条码存在则弹窗
                {
                    if (IndexPage.language == "en-US")
                    {
                        MessageBox.Show("Barcode repeat!");
                    }
                    else
                    {
                        MessageBox.Show("条码重复!");
                    }
                }
                BatchGener();//批量插入条码
                this.Close();//关闭当前窗口
            }
            
        }
        /// <summary>
        /// 点击取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        #region 电池条码及输入终止序号格式检查
        /// <summary>
        /// 检查终止序号格式
        /// </summary>
        /// <param name="end"></param>
        /// <returns></returns>
        public static bool CheckEndNum(string end)
        {
            bool endflag = false;
            if (end != "")//检验终止序号格式
            {
                if (Util.IsBatNum(end))//验证是否是两位以下数字
                {
                    try
                    {
                        endNum = Convert.ToInt32(end);
                        if (endNum < startNum)
                        {
                            if (IndexPage.language == "en-US")
                            {
                                MessageBox.Show("End ID must larger than start ID！");
                            }
                            else
                            {
                                MessageBox.Show("终止序号不能小于起始序号！");
                            }
                        }
                        else if (endNum > 16)
                        {
                            if (IndexPage.language == "en-US")
                            {
                                MessageBox.Show("End ID must less than 16！");
                            }
                            else
                            {
                                MessageBox.Show("终止序号必须小于等于16！");
                            }
                        }
                        else//终止序号输入正确
                        {
                            endflag = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + ex.StackTrace);
                    }
                }
            }
            else
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Please input a positive integer!");
                }
                else
                {
                    MessageBox.Show("请输入一个正整数!");
                }
            }
            return endflag;
        }
        /// <summary>
        /// 检查条码格式
        /// </summary>
        /// <param name="bar"></param>
        /// <returns></returns>
        public static bool CheckBarcode(string bar)
        {
            bool barflag = false;
            if (Util.IsBatBarcode(bar))
            {
                barflag = true;
            }
            else
            {
                barflag = false;
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Please input correct barcode!");
                }
                else
                {
                    MessageBox.Show("请输入正确的条码!");
                }
            }
            return barflag;
        }
        /// <summary>
        /// 检查电池条码是否存在
        /// </summary>
        /// <param name="bar"></param>
        /// <returns></returns>
        public static bool CheckBarcordExisted(string bar)
        {
            if (chlList.Count > 0)
            {
                foreach (Channel chl in chlList)
                {
                    if (chl.Batcode == bar)//若电池条码已存在则返回true
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion
        #region 处理方法
        /// <summary>
        /// 批量生成条码
        /// </summary>
        public void BatchGener()
        {
            try
            {
                string front = startBar.Substring(0,10);//起始条码的前十位
                int bar = Convert.ToInt32(startBar.Substring(10));//后两位
                for (int i = startNum - 1; i < endNum; i++)
                {
                    if (bar < 10)//不足两位用0补足十位
                    {
                        chlList[i].Batcode = front + "0" + bar;//10位字符+2位数字
                    }
                    else
                    {
                        chlList[i].Batcode = front + bar;//10位字符+2位数字
                    }
                    bar++;
                }
                OnWeekBarcode();//触发方法刷新表格
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
        }
        /// <summary>
        /// 触发委托方法
        /// </summary>
        public void OnWeekBarcode()
        {
            if (UpdateTreeList != null)
            {
                UpdateTreeList();
            }
        }
        #endregion
        #region 国际化
        private void BatchGenerBarcode_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("GenerateBatchBarcode");
            this.lblStartNum.Text = ResourceCulture.GetString("StartID");
            this.lblEndNum.Text = ResourceCulture.GetString("EndID");
            this.lblStartBar.Text = ResourceCulture.GetString("StartBar");
            this.btnOk.Text = ResourceCulture.GetString("LoginOk");
            this.btnCancel.Text = ResourceCulture.GetString("LoginCancel");
        }
        #endregion
    }
}
